package com.zhiyiang.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiyiang.pojo.User;
import com.zhiyiang.service.user.UserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/getloginuser/{usercode}")
    public User doGetLoginUser(@PathVariable("usercode") String usercode){
        return userService.getUser(usercode);
    }
    //获取用户列表
    @GetMapping (value = "/findAll/{current}/{size}")
    @ResponseBody
    public Page<User> doUser(@PathVariable("current") Integer current,@PathVariable("size") Integer size){
        Page<User> allUser = userService.getAllUser(current, size);
        return allUser;
    }

    //根据用户名查询用户查询
    @GetMapping (value = "/findUser/{query}/{current}/{size}")
    @ResponseBody
    public Page<User> doUser(@PathVariable("query") String query,@PathVariable("current") Integer current,@PathVariable("size") Integer size){
        Page<User> allUser = userService.getUser(query,current,size);
        return allUser;
    }

    //添加用户
    @PostMapping (value = "/addUser")
    @ResponseBody
    public Boolean addUser(@RequestBody User user){
        Boolean flag = userService.addUser(user);
        return flag;
    }
    //获取当前行用户
    @GetMapping(value = "/getUser/{usercode}")
    @ResponseBody
    public User getUser(@PathVariable("usercode") String usercode){
        User user = userService.getUser(usercode);
        return user;
    }
    //更新用户
    @PutMapping(value = "updateUser/{usercode}")
    @ResponseBody
    public Boolean updateUser(@PathVariable("usercode") String usercode,
                              @RequestBody User user){
        Boolean flag = userService.updateUser(usercode, user);
        return flag;
    }
    //删除当前行用户
    @GetMapping(value = "/deleteUser/{usercode}")
    @ResponseBody
    public Boolean deleteUser(@PathVariable("usercode") String usercode){
        Boolean flag = userService.deleteUser(usercode);
        return flag;
    }


}
